package weaver.interfaces.solead.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.solead.utils.SoleadUtil;
import weaver.interfaces.solead.wcf.WcfUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-08-26
 * @Description: 客户业务员维护审批流程结束，调用SAP接口生成采购订单
 * @ModifyLog:
 **/

public class WeaKhywywhspAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        writeLog( ">>>>WeaKhywywhspAction=" + requestid );
        String message = "";
        try {
            String sql = "select id,ywhbdm,ywhbmcwb,xzkhqc,xzywhbdm,zdm,gsbh,jskhxjzh,jskpdzjdh,xy,ysyld,sfxzkh " +
                    "from " + tablename + " where requestid=?";
            rs.executeQuery( sql, requestid );
            rs.next();
            String mainid = Util.null2String( rs.getString( "id" ) );
            //是否新增业务伙伴
            String sfxzkh = Util.null2String( rs.getString( "sfxzkh" ) );
            //业务伙伴代码
            String ywhbdm = Util.null2String( rs.getString( "ywhbdm" ) );
            String key = "0".equals(sfxzkh)?"":ywhbdm;
            //新增业务伙伴名称
            String xzkhqc = Util.null2String( rs.getString( "xzkhqc" ) );
            //新增业务伙伴代码
            String xzywhbdm = Util.null2String( rs.getString( "xzywhbdm" ) );
            //业务伙伴名称文本
            String ywhbmcwb = Util.null2String( rs.getString( "ywhbmcwb" ) );
            //业务伙伴类型
            String ywhblxwb = "C";
            //组代码
            String zdm = Util.null2String( rs.getString( "zdm" ) );
            //国税编号
            String gsbh = Util.null2String( rs.getString( "gsbh" ) );
            //金税开户行及账号
            String jskhxjzh = Util.null2String( rs.getString( "jskhxjzh" ) );
            //金税开票地址及电话
            String jskpdzjdh = Util.null2String( rs.getString( "jskpdzjdh" ) );
            //行业
            String xy = Util.null2String( rs.getString( "xy" ) );
            //月使用量(吨)
            String ysyld = Util.null2String( rs.getString( "ysyld" ) );
            JSONObject header = new JSONObject();
            if("0".equals(sfxzkh)){
                header.put( "CardCode", xzywhbdm );
                header.put( "CardName", xzkhqc );
            }else {
                header.put( "CardName", ywhbmcwb );
            }

            header.put( "CardType", ywhblxwb );
            header.put( "GroupCode", zdm );
            header.put( "FederalTaxID", gsbh );
            header.put( "GTSBankAccountNo", jskhxjzh );
            header.put( "GTSBillingAddrTel", jskpdzjdh );
            header.put( "U_Trade", xy );
            header.put( "U_MQty", ysyld );
            writeLog( ">>>>>header=" + header.toJSONString() );
            JSONArray bodyList1 = new JSONArray();
            String sql1 = "select lxr,dh from " + tablename + "_dt1  where  mainid=?";
            rs.executeQuery( sql1, mainid );
            while (rs.next()) {
                //联系人
                String lxr = Util.null2String( rs.getString( "lxr" ) );
                //电话
                String dh = Util.null2String( rs.getString( "dh" ) );
                JSONObject body = new JSONObject();
                body.put( "Name", lxr );
                body.put( "Phone1", dh );
                writeLog( ">>>>>body=" + body.toJSONString() );
                bodyList1.add( body );
            }
            writeLog( ">>>>>bodyList1=" + bodyList1.size() );
            JSONArray bodyList2 = new JSONArray();
            String sql2 = "select dzbs,xxdz from " + tablename + "_dt2  where  mainid=?";
            rs.executeQuery( sql2, mainid );
            while (rs.next()) {
                //地址标识
                String dzbs = Util.null2String( rs.getString( "dzbs" ) );
                //详细地址
                String xxdz = Util.null2String( rs.getString( "xxdz" ) );
                JSONObject body = new JSONObject();
                body.put( "AddressName", dzbs );
                body.put( "BuildingFloorRoom", xxdz );
                writeLog( ">>>>>body=" + body.toJSONString() );
                bodyList2.add( body );
            }
            writeLog( ">>>>>bodyList2=" + bodyList2.size() );
            String param = SoleadUtil.ywhb_XMLContent( header, bodyList1, bodyList2 );
            Map<String, String> resultMap = WcfUtil.send( param, "solead", "2",key);
            String code = Util.null2String( resultMap.get( "code" ) );
            String msg = Util.null2String( resultMap.get( "msg" ) );
            if (!"0".equals( code )) {
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "Action接口异常," + msg );
                return Action.FAILURE_AND_CONTINUE;
            } else {
                writeLog( ">>>>>code=" + code+",msg="+msg);
                return Action.SUCCESS;
            }
        } catch (Exception e) {
            message = "Action 接口异常！！！";
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( message );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
